<?php
if ( !class_exists( "Classes_Mysql" ) ) { 
/*

	Author:	Cedric McMillan Junior

	Date:	March 15th 2012
	
	Aim:	Mysql class with some added loggging/profiling and error handling (exceptions)
	

*/

# for production server we will just log to a file
class dbException extends Exception
{
    public function getErrorReport() 
	{
		$msg	=	"Mysql Error: \n";

		$array	=	$this->getTrace();

		for ($i=0;$i<count($array);$i++)
		{
			$msg	.=
				$this->getMessage() 
			.	" "
		    .	mysql_error()
			.	" "
			.	"File: "
			.	$array[$i]['file']
			.	" "
			.	"Line: " 
			.	$array[$i]['line']
			.	"\n";
			
		}
		# store these comments at all times
		addComment($msg,ERROR_LOG_FILE);

		# if debugging, show users
		if	(MYSQL_DEBUG_MODE == true)
		{
			echo	nl2br($msg);
		}

		# this bit is debatable
		die;
	}
}


/*
	
	Mysql class.
	
	Also allows some basic profiling
	and contains some exceptions to assist error checks


*/


class Classes_Mysql 
{
	private $queries,	$id;
  
	private	$user	=	MYSQL_USER;
	private	$pass	=	MYSQL_PASS;
	private	$db		=	MYSQL_DBASE;
	private	$host	=	MYSQL_HOST;
	
	private	$link			=	NULL;
	
	# constructor
    public function __construct() 
	{
		$this->link		=	mysql_connect($this->host, $this->user, $this->pass);
		try
		{
			if	(!$this->link)
			{
	            throw new Exception ("mysql_connect");
			}
		}
		catch	(Exception $e)
		{
			$e->getErrorReport();
		}
	
	

		try
		{
			if	(!mysql_select_db($this->db, $this->link))
			{
	            throw new Exception ("mysql_select_db");
			}
		}
		catch	(Exception $e)
		{
			$e->getErrorReport();
		}
	
    }




	# close connection
	function close()
	{
		mysql_close($this->link);
	}





    # Use this function if the query will only return a
    # single data element.
    public function queryItem($query,$file,$line) 
	{
		$start	=	$this->getTime();
		try
		{
            $result = mysql_query($query, $this->id);
			if	(!$result)
			{
	            throw new Exception ("queryItem");
			}
			$data		=	mysql_fetch_row($result) or
			$data[0]	=	"";
		    mysql_free_result($result);
			# log for profiler if necessary
			if	(PQP_DEBUG)
			{
				$this->logQuery($query, $start,$file,$line);
			}
			return($data[0]);
		}
		catch	(Exception $e)
		{
			$e->getErrorReport();
		}
    }





    # This function is useful if the query will only return a
    # single row.
    public function queryRow ($query,$file,$line) 
	{
		$start	=	$this->getTime();
		try
		{
            $result = mysql_query($query, $this->id);
			if	(!$result)
			{
	            throw new Exception ("queryRow");
			}
			$data		=	mysql_fetch_assoc($result) or "";
			# log for profiler if necessary
			if	(PQP_DEBUG)
			{
				$this->logQuery($query, $start,$file,$line);
			}
			return($data);
		}
		catch	(Exception $e)
		{
			$e->getErrorReport();
		}
	}
	
	
	

	# insert a row
    public function insert ($query,$file,$line) 
	{
		$start	=	$this->getTime();
		try
		{
            $result = mysql_query($query, $this->id);
			if	(!$result)
			{
	            throw new Exception ("insert");
			}
			# log for profiler if necessary
			if	(PQP_DEBUG)
			{
				$this->logQuery($query, $start,$file,$line);
			}
		}
		catch	(Exception $e)
		{
			$e->getErrorReport();
		}
	}





	# returns last id ater insert
    public function getInsertID () 
	{
        return mysql_insert_id();
    }





	# update table
    public function update ($query,$file,$line) 
	{
		$start	=	$this->getTime();
		try
		{
            $result = mysql_query($query, $this->id);
			if	(!$result)
			{
	            throw new Exception ("update");
			}
			# log for profiler if necessary
			if	(PQP_DEBUG)
			{
				$this->logQuery($query, $start,$file,$line);
			}
		}
		catch	(Exception $e)
		{
			$e->getErrorReport();
		}

    }





    public function delete ($query,$file,$line) 
	{
		$start	=	$this->getTime();
		try
		{
            $result = mysql_query($query, $this->id);
			if	(!$result)
			{
	            throw new Exception ("delete");
			}
			
			# log for profiler if necessary
			if	(PQP_DEBUG)
			{
				$this->logQuery($query, $start,$file,$line);
			}
			
		}


		catch	(Exception $e)
		{
			$e->getErrorReport();
		}
    }






	# Returns an assoc array 
	function selectToArray($query,$file="",$line="") 
	{
		$start	=	$this->getTime();
		try
		{
            $result =    @mysql_query($query, $this->link);
			if	(!$result)
			{
	            throw new dbException ("selectToArray");
			}
	
			$got	=	array();

			while ($row = @mysql_fetch_assoc($result))
			{
				array_push($got, $row);
			}
			
			# log for profiler if necessary
			if	(PQP_DEBUG)
			{
				$this->logQuery($query, $start,$file,$line);
			}
			return $got;
		}
		catch	(Exception $e)
		{
			$e->getErrorReport();
		}
	}





	# stores the query and time in an array to be accessed by profiler
	private function logQuery(&$sql, &$start,&$file,&$line) 
	{
		# interface of the PQP profiler
		global	$gQueriesArray;
		global	$gQueries;
		$gQueries++;

		$query = array(
			'sql' => $sql,	
			'file' => $file,	
			'line' => $line,	
			'time' => ($this->getTime() - $start)*1000   );

		array_push($gQueriesArray, $query);
	}





	# microtime for profiler
	private function getTime() 
	{
		$time = microtime();
		$time = explode(' ', $time);
		$time = $time[1] + $time[0];
		$start = $time;
		return $start;
	}



}




























































































}


?>